#!/bin/sh
#
# Keep the main clock in powerdown until used
#

source /etc/device_config

handle_calibration_files() {
	if [[ -s /mnt/jffs2/${CALIBFILENAME} ]]; then
		cp /mnt/jffs2/${CALIBFILENAME} /opt/${CALIBFILENAME}
	elif [[ -s /mnt/jffs2/${CALIBFILENAME_FACTORY} ]]; then
		cp /mnt/jffs2/${CALIBFILENAME_FACTORY} /opt/${CALIBFILENAME}
	fi

	if [[ -s /mnt/jffs2/${CALIBFILENAME_TEMP_LUT} ]]; then
		cp /mnt/jffs2/${CALIBFILENAME_TEMP_LUT} /opt/${CALIBFILENAME_TEMP_LUT}
	fi
}

case "$1" in
 start)
	# Restore saved password and Dropbear keys
	[[ -d /mnt/jffs2/etc ]] && cd /mnt/jffs2/etc && md5sum -s -c password.md5 && cp passwd shadow group /etc
	[[ -d /mnt/jffs2/etc/dropbear ]] && cd /mnt/jffs2/etc/dropbear && md5sum -s -c keys.md5 && cp dropbear* /etc/dropbear/
	[[ -d /mnt/jffs2/root/.ssh ]] && cd /mnt/jffs2/root/.ssh && md5sum -s -c keys.md5 && mkdir /root/.ssh && cp authorized_keys /root/.ssh
	iio_attr -q -d m2k-fabric clk_powerdown 1
	handle_calibration_files
	;;
  stop)
	;;
  restart|reload)
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
